#include "SDL/SDL.h"
#include "Game.h"
#include "Timer.h"
#include <string>
//#include <crtdbg.h>

const short SCREENWIDTH = 640;
const short SCREENHEIGHT = 480;
const short SCREENBPP = 32;
const short FPS = 60;

SDL_Surface *screen = NULL;
SDL_Event events; //init event handler
Game* game;

bool init()
{
	if (SDL_Init( SDL_INIT_EVERYTHING ) == -1) //init alles van SDL, bvb.: events
		return false;

	screen = SDL_SetVideoMode(SCREENWIDTH, SCREENHEIGHT, SCREENBPP, SDL_SWSURFACE);
	if (screen == NULL)
		return false;
	
	game = new Game(); //initialiseren van variabelen van Game

	return true;
}

void Quit()
{
	SDL_FreeSurface(screen);
    delete game;
	SDL_Quit();
}

int main( int argc, char* args[] )
{
	if (init() == false)
		return 1;
	
	if (game->load() == false) //preloaden van content (images, geluid, ...)
	{
		Quit();
		return 1;
	}

	Timer* fpsTimer = new Timer(), *fpsCountTimer = new Timer();
	fpsCountTimer->start();
	short frames = 0, fps = FPS;
	
	bool quit = false;	
	while (!quit)
	{
		fpsTimer->start();
		
		if (fpsCountTimer->getTicks() >= 1000) // = 1sec
		{
			fps = frames;
			frames = 0;
			fpsCountTimer->start();
		}

		if (game->update(events, fps) == false)
			quit = true;
		
		game->draw(screen, fps);
		
		frames++;
		if (fpsTimer->getTicks() < 1000 / FPS)
			SDL_Delay(1000 / FPS - fpsTimer->getTicks());
	}

    Quit();
	//_CrtDumpMemoryLeaks();
    return 0;    
}
